﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// qiugou_detail 的摘要说明
    /// </summary>
    public class gongying_detail : IHttpHandler, IRequiresSessionState
    {
        protected int id = 0;
        public void ProcessRequest(HttpContext context)
        {
            id = DTRequest.GetQueryInt("id", 0);
            if (id == 0)
            {
                return;
            }
            //检查用户是否登录
            int _user_id = DTRequest.GetFormInt("txtUserID", 0);

            int is_view = 0;
            int _group_id = 0;
            if (_user_id > 0)
            {
                _group_id = new BLL.users().GetGroupID(_user_id); //获取groupid
                if (_group_id > 1)
                {
                    is_view = 1;
                }
                else if (_group_id == 1)
                {
                    int cur_num = new BLL.gongqiu_log().GetCount("user_id=" + _user_id + " and datediff(d,add_time,getdate())=0");
                    if (cur_num < 20)
                    {
                        is_view = 1;
                    }
                }
            }

            BLL.gongying bll = new BLL.gongying();
            Model.gongying model = bll.GetModel(id);
            //更新阅读数
            bll.UpdateField(id, "hits=hits+1");
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            strTxt.Append("\"user_id\":" + model.user_id + ",");
            strTxt.Append("\"is_view\":" + is_view + ",");
            strTxt.Append("\"title\":\"" + model.title + "\",");
            strTxt.Append("\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(model.add_time)) + "\",");
            if (model.price > 0)
            {
                strTxt.Append("\"market_price\":\"" + model.price + "元/" + model.unit + "\",");
            }
            else
            {
                strTxt.Append("\"market_price\":\"面议\",");
            }
            
            strTxt.Append("\"size\":\"" + model.size_long + "×" + model.size_wide + "×"+model.size_height+"mm\",");
            strTxt.Append("\"remark\":\"" + model.content.ToString() + "\",");
            strTxt.Append("\"hits\":" + model.hits + ",");
            if (model.user_id > 0)
            {
                Model.users userModel = new BLL.users().GetModel(model.user_id);
                if (userModel != null)
                {
                    strTxt.Append("\"lianxiren\":\"" + userModel.user_name + "\",");
                    if (userModel.avatar.Contains("shicaiquanzi"))
                    {
                        strTxt.Append("\"avatar\":\"http://" + userModel.avatar + "\",");
                    }
                    else
                    {
                        strTxt.Append("\"avatar\":\"http://pic.shicaiquanzi.com" + userModel.avatar + "\",");
                    }
                    strTxt.Append("\"mobile\":\"" + userModel.mobile + "\",");
                }
                //string _username = new BLL.users().GetUserName(model.user_id);
                //string _useravatar=new
            }
            else
            {
                strTxt.Append("\"lianxiren\":\"" + model.lianxiren.ToString() + "\",");
                strTxt.Append("\"avatar\":\"../image/default_avatar.png\",");
                strTxt.Append("\"mobile\":\"" + model.mobile.ToString() + "\",");
            }
            
            string imglist=string.Empty;
            if(model.albums!=null)
            {
                 foreach(Model.common_albums modelt in model.albums)
                 {
                     if (modelt.original_path.Contains("shicaiquanzi"))
                     {
                         imglist += "<div class=\"pic\"><img src=\"http://" + modelt.original_path + "\"/></div>";
                     }
                     else
                     {
                         imglist += "<div class=\"pic\"><img src=\"http://www.shicaiquanzi.com" + modelt.original_path + "\"/></div>";
                     }
                 }
            }
            if (!string.IsNullOrEmpty(imglist))
            {
                strTxt.Append("\"imglist\":\"" + Microsoft.JScript.GlobalObject.escape(imglist) + "\"");
            }
            else
            {
                strTxt.Append("\"imglist\":\"\"");
            }
            //判断是否有传过来用户名、密码
            if (new BLL.collection().Exists(2, id, _user_id))
            {
                strTxt.Append(",\"is_collect\":1");
            }
            else
            {
                strTxt.Append(",\"is_collect\":0");
            }

            //推荐内容
            strTxt.Append(",\"tuijian_list\":[");
            BLL.gongying bll2 = new BLL.gongying();
            DataSet ds = bll2.GetList(0, "size_long=" + model.size_long + " and size_wide=" + model.size_wide + " and size_height=" + model.size_height+" and id<>"+model.id, "id desc");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    strTxt.Append(",\"size\":\"" + dr["size_long"] + "×" + dr["size_wide"] + "×" + dr["size_height"] + "\"");
                    if (string.IsNullOrEmpty(dr["img_url"].ToString()))
                    {
                        strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                    }
                    else
                    {
                        string picurl = dr["img_url"].ToString();
                        if (picurl.Contains("shicaiquanzi"))
                        {
                            strTxt.Append(",\"img_url\":\"http://" + picurl + "\"");
                        }
                        else
                        {
                            strTxt.Append(",\"img_url\":\"http://www.shicaiquanzi.com" + picurl + "\"");
                        }
                    }
                    strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
                    strTxt.Append(",\"lianxiren\":\"" + dr["lianxiren"] + "\"");
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
            }
            strTxt.Append("]");

            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
            if (_user_id > 0 && _group_id==1)
            {
                new BLL.gongqiu_log().Add(1, id, _user_id);
                new BLL.gongqiu_log().Delete("datediff(d,add_time,getdate())>0");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}